<html>
<head><meta charset="utf-8"><title>&#x27;rustc&#x27; panicked at &#x27;could not lift for printing&#x27; · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html">&#x27;rustc&#x27; panicked at &#x27;could not lift for printing&#x27;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218999604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/218999604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#218999604">(Dec 06 2020 at 19:09)</a>:</h4>
<p>I don't really know where to start. I'm writing a tool that re-use rust internals.</p>
<p>I'm trying to print a <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/consts/struct.Const.html"><code>ty::Const</code></a>. It seems that I could just do <code>write!("{}", my_const)</code> but I get the following error: <a href="https://github.com/rust-lang/rust/blob/1c389ffef/compiler/rustc_middle/src/ty/print/pretty.rs#L1844">"could not lift for printing"</a>. It seems that <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.mk_const">TyCtxt::mk_const()</a> could help, but I get a weird borrow error if I try to use it.</p>
<p>My code is here: <code>git clone git@github.com:robinmoussu/cargo-callgraph.git</code>. More specifically, it's the line <a href="https://github.com/robinmoussu/cargo-callgraph/blob/master/extract_dependencies.rs#L649">649</a> and <a href="https://github.com/robinmoussu/cargo-callgraph/blob/master/extract_dependencies.rs#L675">675</a> of <code>extract_dependencies.rs</code>.</p>
<p>In order to run it, a simple <code>cargo run -- test/basic.rs &amp;&amp; dot example2.dot -Txlib</code> should suffice¹.</p>
<p>The branch <code>buggy</code> uncomment the <code>writeln!</code> and expose the bug "could not lift for printing".</p>
<p>¹. “Make it work, make it right, make it fast”. I'm still at step 1, some stuff is hardcoded, the code is not cleaned-up, the history is a mess, … I'm sorry. But I'm really close to be able to start step 2 and clean all of this mess <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="219002216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219002216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219002216">(Dec 06 2020 at 19:39)</a>:</h4>
<p><code>AllDependencies</code> names the lifetime param <code>'tcx</code> but then the functiin that uses it uses a separate lifetime param called <code>'dependencies</code> that isn't guaranteed to live at most as long as <code>'tcx</code>, so it may contain <code>Const</code> values that aren't interned into <code>tcx</code>.</p>



<a name="219002995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219002995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219002995">(Dec 06 2020 at 19:44)</a>:</h4>
<p>Before I tried to fix this. do you think that using <code>mk_const</code> is what I should do?</p>



<a name="219003667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219003667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219003667">(Dec 06 2020 at 19:48)</a>:</h4>
<p>and what does "interned" means?</p>



<a name="219004207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219004207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219004207">(Dec 06 2020 at 19:51)</a>:</h4>
<p>Yes, <code>mk_const</code> interns <code>Const</code>. Interning means that a single copy is stored in <code>TyCtxt</code> for each used value. This makes the <code>PartialEq</code> impl a matter of a pointer comparison and saves memory when the type is bigger than a pointer.</p>



<a name="219006029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219006029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219006029">(Dec 06 2020 at 20:01)</a>:</h4>
<p>The way my algorithm works is more or less:</p>
<ul>
<li>iterate over <code>tcx.body_owner()</code></li>
<li>create reference to the various variables I need, and store them in <code>AllDependencies</code></li>
<li>iterate over <code>AllDependencies</code>, and generate a textual representation</li>
</ul>
<p>In order to do the 3rd step, I need to have the constants interned. But the borrow checker complains that<code>AllDependencies</code> doesn't live long enough if I try to interns them.</p>



<a name="219006210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219006210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219006210">(Dec 06 2020 at 20:02)</a>:</h4>
<p>And I don't understand why it should be the case, nor why it complains (<code>AllDependencies</code> is dropped after the last text is written).</p>



<a name="219006478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219006478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219006478">(Dec 06 2020 at 20:03)</a>:</h4>
<p>For me <code>tcx</code> should outlive <code>AllDependencies</code>, not the reverse.</p>



<a name="219025859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/%27rustc%27%20panicked%20at%20%27could%20not%20lift%20for%20printing%27/near/219025859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/&#x27;rustc&#x27;.20panicked.20at.20&#x27;could.20not.20lift.20for.20printing&#x27;.html#219025859">(Dec 06 2020 at 22:04)</a>:</h4>
<p>I had a lifetime bug in the specification of one of my extraction function. It's fixed!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>